<!-- 弹窗 详情 -->
<script lang="tsx" setup>
import FileUpload from "@/components/file-system/file-upload.vue";
import type { FormDescChild } from "@/components/form-desc/form-desc.vue";
import { whManagerBsTbEnterpriseSaveOrUpdateUsingPost } from "@/genapi/wh";
import { useAsync } from "@/hooks/use-async";
import {
	ECONOMIC_TYPE_OPTIONS,
	SUPERVISE_TYPE_OPTIONS,
	INDUSTRY_OPTIONS,
} from "@/config/enum-map";

import { injectState } from "../state";

defineOptions({ name: "DetailDialog" });

const { page } = injectState()!;

const dialogRef = shallowRef<InstanceType<GlobalComponents["PromiseDialog"]>>();
const formDescRef = shallowRef<InstanceType<GlobalComponents["FormDesc"]>>();

const type = ref<"write" | "read">();

const beforeClose = async () => {
	if (type.value === "write") {
		await ElMessageBox.confirm("确认关闭？");
	}
};

const trigger = async (_type: "write" | "read" = "read", value = {} as any) => {
	type.value = _type;
	model.value = JSON.parse(JSON.stringify(value)) as any;
	await dialogRef.value?.open();
};

defineExpose({
	trigger,
});

const model = ref<(typeof page.records.value)[0]>({});

const file = computed({
	get() {
		if (model.value.fileId) {
			return [
				{
					id: model.value.fileId,
					name: model.value.fileName,
				},
			];
		}
	},
	set(value: any) {
		model.value.fileId = value?.[0]?.id;
	},
});

const title = computed(() => {
	if (type.value === "write") {
		return model.value.id ? "编辑" : "新增";
	} else {
		return `详情`;
	}
});

const { execute, isLoading } = useAsync(async () => {
	await formDescRef.value?.formRef?.validate();
	await ElMessageBox.confirm("确认提交？", "提示", {
		type: "warning",
	});

	await whManagerBsTbEnterpriseSaveOrUpdateUsingPost({
		data: {
			...model.value,
		},
	});
	ElMessage.success("提交成功");
	page.execute();
	dialogRef.value?.complete();
});

const children: FormDescChild[] = [
	{
		label: "企业名称",
		prop: "name",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.name} placeholder="请输入" />
		),
		descItem: () => `${model.value.name ?? "-"}`,
	},
	{
		label: "主要负责人",
		prop: "mainMasterName",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.mainMasterName} placeholder="请输入" />
		),
		descItem: () => `${model.value.mainMasterName ?? "-"}`,
	},
	{
		label: "主要负责人电话",
		prop: "mainMasterPhone",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.mainMasterPhone} placeholder="请输入" />
		),
		descItem: () => `${model.value.mainMasterPhone ?? "-"}`,
	},
	{
		label: "安全生产许可编号",
		prop: "safeProductAuthno",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.safeProductAuthno} placeholder="请输入" />
		),
		descItem: () => `${model.value.safeProductAuthno ?? "-"}`,
	},
	{
		label: "从业人员人数",
		prop: "employeeNum",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.employeeNum} placeholder="请输入" />
		),
		descItem: () => `${model.value.employeeNum ?? "-"}`,
	},
	{
		label: "经济类型",
		prop: "economicType",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-select-v2
				class="w-100%"
				options={[...ECONOMIC_TYPE_OPTIONS]}
				v-model={model.value!.economicType}
				placeholder="请选择"
			/>
		),
		descItem: () => `${model.value.economicType ?? "-"}`,
	},
	{
		label: "所属行业",
		prop: "industry",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-select-v2
				class="w-100%"
				options={[...INDUSTRY_OPTIONS]}
				v-model={model.value!.industry}
				placeholder="请选择"
			/>
		),
		descItem: () => `${model.value.industry ?? "-"}`,
	},
	{
		label: "监管分类",
		prop: "superviseType",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-select-v2
				class="w-100%"
				options={[...SUPERVISE_TYPE_OPTIONS]}
				v-model={model.value!.superviseType}
				placeholder="请选择"
			/>
		),
		descItem: () => `${model.value.superviseType ?? "-"}`,
	},
];
</script>

<template>
	<PromiseDialog
		ref="dialogRef"
		:title="title"
		:before-close="beforeClose"
		w="707px!"
	>
		<el-scrollbar h="408px!">
			<FormDesc
				ref="formDescRef"
				m="t-24px x-30px"
				:type="type === 'read' ? 'desc' : 'form'"
				form-class="gap-x-20px"
				:model="model"
				:children="children"
				:column="2"
			/>
		</el-scrollbar>
		<template v-if="type === 'write'" #footer>
			<el-button @click="dialogRef?.close()">取消</el-button>
			<el-button type="primary" :is-loading="isLoading" @click="execute()"
				>确定</el-button
			>
		</template>
	</PromiseDialog>
</template>
